﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using Common;
using DAL;
using System.Data.Linq;
namespace BAL
{
    public class Menus
    {
        internal static MenuInfo TransMenu2MenuInfo(Models.Menu menu)
        {
            MenuInfo menuInfo = new MenuInfo();

            Utility.SetObjectValueWithTheSamePropertyName(menuInfo, menu);

            return menuInfo;
        }
        public static MenuInfo GetMenuInfoByPage(string page)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                Models.Menu menu = db.Menus.SingleOrDefault(w => w.Page == page);
                if (menu != null)
                    return TransMenu2MenuInfo(menu);
                else
                    return null;
            }
        }
        public static MenuInfo AddMenu(MenuInfo menuInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                Models.Menu menu = new Models.Menu();
                Utility.SetObjectValueWithTheSamePropertyName(menu, menuInfo);
                DatabaseHelper.Insert<Models.Menu>(menu);
                return TransMenu2MenuInfo(menu);
            }
        }
        public static void EditMenu(MenuInfo menuInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                try
                {
                    Models.Menu menu = db.Menus.Single(b => b.Id == menuInfo.Id);

                    Utility.SetObjectValueWithTheSamePropertyName(menu, menuInfo);

                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                }
                catch (ChangeConflictException)
                {
                    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
                    {
                        occ.Resolve(RefreshMode.KeepCurrentValues);
                    }
                }
            }
        }
        public static IList<MenuInfo> GetMenuInfos(IList<int> menuIds)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.Menus.Where(s => GetArray<int>.Get(menuIds).Contains(s.Id)).OrderBy(s=>s.OrderId).Select(s => TransMenu2MenuInfo(s)).ToList();
            }
        }
        public static IList<MenuInfo> GetMenuInfos(int first, int max)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (max > 0)
                {
                    return db.GetTable<Models.Menu>().OrderBy(c => c.OrderId).Skip(first).Take(max).Select(c => TransMenu2MenuInfo(c)).ToList();
                }
                else
                {
                    return db.GetTable<Models.Menu>().OrderBy(c => c.OrderId).Select(c => TransMenu2MenuInfo(c)).ToList();
                }
            }
        }
        public static int GetMenuInfoCount()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<Models.Menu>().Count();
            }
        }
        public void Update(int id, string name,string page,int orderid)
        {

        }
        public static MenuInfo GetMenuInfo( int ID)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return TransMenu2MenuInfo(db.Menus.Single(t => t.Id == ID));
            }
        }

        public void Delete(int id)
        {

        }
        public static void DelMenu(int id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                DelMenu(new int[] { id });
            }
        }

        public static void DelMenu(int[] lstId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (lstId.Length > 0)
                {
                    string sql = string.Format("delete from Menu where id in ({0})", string.Join(",", lstId.ConvertToStringArray()));
                    db.ExecuteCommand(sql);
                }
            }
        }
        public static bool CheckMenuExistById(int Id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.Menus.Where(w => w.Id == Id).Count() > 0;
            }
        }
        public static bool CheckMenuExistByName(string name)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.Menus.Where(w => w.Name == name).Count() > 0;
            }
        }
    }
}
